﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Model
{
    /// <summary>
    /// Entity转数据字典
    /// </summary>
    public static class DbFieldValues
    {

        public static IDictionary<string, object> ToPropertyParam(this object entity)
        {
            IDictionary<string, object> param = new Dictionary<string, object>();
            PropertyInfo[] propertyInfos = entity.GetType().GetProperties();

            foreach (var item in propertyInfos)
            {
                var property = item.Name;


                if (!param.ContainsKey(property))
                {
                    param.Add(property, item.GetValue(entity, null));
                }
            }
            return param;
        }
        //add by caism 20180724
        public static IDictionary<string, object> ToPropertyParamNoIDKey(this object entity,string IDKey)
        {
            IDictionary<string, object> param = new Dictionary<string, object>();
            PropertyInfo[] propertyInfos = entity.GetType().GetProperties();

            foreach (var item in propertyInfos)
            {
                var property = item.Name;
                if (item.Name.ToLower() == IDKey.ToLower()) continue;

                if (!param.ContainsKey(property))
                {
                    param.Add(property, item.GetValue(entity, null));
                }
            }
            return param;
        }

    }
}
